/**
 * Copyright (c) 2024-2025 National Fundamental Software of China Co., Ltd.
 *
 * You should have find a copy of NFSChina License. If not, please contact us
 * by email <os_support@nfschina>
 *
**/

import QtQuick 2.0
import QtQuick.Layouts 1.1
import org.kde.plasma.plasmoid 2.0
import org.kde.plasma.core 2.0 as PlasmaCore

Item {
	id: widget

	property bool disableLatteParabolicIcon: true
	readonly property bool vertical: plasmoid.formFactor === PlasmaCore.Types.Vertical
	readonly property real scaleFactor: PlasmaCore.Units.devicePixelRatio

	Plasmoid.onActivated: widget.activate()

	Plasmoid.preferredRepresentation: Plasmoid.fullRepresentation
	Plasmoid.fullRepresentation: MouseArea {
		id: panelItem

		readonly property bool inPanel: (plasmoid.location == PlasmaCore.Types.TopEdge
			|| plasmoid.location == PlasmaCore.Types.RightEdge
			|| plasmoid.location == PlasmaCore.Types.BottomEdge
			|| plasmoid.location == PlasmaCore.Types.LeftEdge)

		Layout.minimumWidth: {
			switch (plasmoid.formFactor) {
			case PlasmaCore.Types.Vertical:
				return 0;
			case PlasmaCore.Types.Horizontal:
				return height;
			default:
				return PlasmaCore.Units.gridUnit * 3;
			}
		}

		Layout.minimumHeight: {
			switch (plasmoid.formFactor) {
			case PlasmaCore.Types.Vertical:
				return width;
			case PlasmaCore.Types.Horizontal:
				return 0;
			default:
				return PlasmaCore.Units.gridUnit * 3;
			}
		}

		Layout.maximumWidth: widget.vertical ? -1 : parent.height
		Layout.maximumHeight: widget.vertical ? parent.width : -1

		onClicked: widget.activate()				
		hoverEnabled: true

		PlasmaCore.IconItem {
			id: iconBackground

			anchors.centerIn: parent
			anchors.margins: 0
			smooth: true
			source: ""
			width: 38 * scaleFactor
			height: 38 * scaleFactor
			roundToIconSize: false
			usesPlasmaTheme: true
		}
		AppletIcon {
			id: icon
			anchors.fill: parent
			source: plasmoid.configuration.icon
			active: panelItem.containsMouse
		}
		states: [
			State {
				name: "defalult"
				when: !panelItem.containsMouse && !panelItem.pressed
			},
			State {
				name: "default-hovered"
				when: panelItem.containsMouse && !panelItem.pressed
			},
			State {
				name: "default-clicked"
				when: panelItem.containsMouse && panelItem.pressed
			}
		]
		onStateChanged: {
			if (state == "default-hovered") {
				iconBackground.source = "task-pinned-hover"
			} else if (state == "default-clicked") {
				iconBackground.source = "task-pinned-click"
			} else {
				iconBackground.source = ""
			}
		}
	}

	PlasmaCore.DataSource {
		id: executable
		engine: "executable"
		connectedSources: []
		onNewData: disconnectSource(sourceName)

		function exec(cmd) {
			executable.connectSource(cmd)
		}
	}

	function action_showOverview() {
		executable.exec('qdbus org.kde.kglobalaccel /component/kwin invokeShortcut "Overview"')
	}

	function activate() {
		if (plasmoid.configuration.clickCommand == 'Overview') {
			action_showOverview()
		} 		
	}

	Component.onCompleted: {
		plasmoid.setAction("showOverview", i18n("Desktop Overview"), "view-grid");
		plasmoid.removeAction("configure");
		plasmoid.removeAction("remove");		
	}
}
